package com.superzqb.asus.superzqbFragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbActivity.Bean.UserInfoBean;
import com.superzqb.asus.superzqbFragment.superzqbMine.AreaInfo;
import com.superzqb.asus.Api.UsersApi;
import com.superzqb.asus.superzqbUtil.GeneralFun;
import com.superzqb.asus.superzqbUtil.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asus on 2017/1/17.
 */

public class MineFragment extends Fragment {
    private TextView logout,name,phone,money,aboutUs;
    private ImageView img;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;
    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.mine, container, false);
        initView(view);
        if(sp.getString("uid",null)!=null)
        getLv();
        return view;
    }

    private void getLv() {
        Retrofit retrofit=new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        UsersApi api=retrofit.create(UsersApi.class);
        Call<UserInfoBean> call=api.getUserInfo(sp.getString("uid",null));
        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                if(response.isSuccessful()){
                    UserInfoBean info=response.body();
                    toInfo(info);
                }

            }
            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable throwable) {
                Toast.makeText(getActivity(),"您没有登录，请登录！",Toast.LENGTH_SHORT).show();
                new GeneralFun().login(getActivity());
            }
        });

    }

    private void toInfo(UserInfoBean info) {
        Picasso.with(getActivity()).load(new Url().getImg_url()+info.getImg()).into(img);
        name.setText("我的昵称:"+info.getName());
        phone.setText("手机号:13081765886");
        money.setText(info.getMoney()+"（元）");
    }

    private void initView(View view) {
        sp=getActivity().getSharedPreferences("uinfo", getActivity().MODE_PRIVATE);
        edit=sp.edit();
        img=(ImageView)view.findViewById(R.id.pic);
        logout=(TextView)view.findViewById(R.id.logout);
        name=(TextView)view.findViewById(R.id.name);
        phone=(TextView)view.findViewById(R.id.phone);
        money=(TextView)view.findViewById(R.id.money);
        aboutUs=(TextView)view.findViewById(R.id.aboutUs);
        logout.setOnClickListener(listen);
        aboutUs.setOnClickListener(listen);
    }
    View.OnClickListener listen= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.logout:
                    String name=sp.getString("name",null);
                    edit.clear();
                    edit.putString("name",name);
                    edit.commit();
                    Toast.makeText(getActivity(),"注销成功！",Toast.LENGTH_SHORT).show();
                    new GeneralFun().login(getActivity());
                    break;
                case R.id.aboutUs:
                    Intent intent=new Intent(getActivity(),AreaInfo.class);
                    getActivity().startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
    }
}
